package com.app.micaihu.view.user.usertask;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Layout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.collection.ArrayMap;

import com.android.volley.VolleyError;
import com.app.http.NetObserver;
import com.app.http.RxLifecycleUtils;
import com.app.http.bean.BaseBean;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.AdjutantMsg;
import com.app.micaihu.bean.infor.GetTaskBean;
import com.app.micaihu.bean.infor.SignLasting;
import com.app.micaihu.bean.infor.TaskSignBean;
import com.app.micaihu.bean.infor.UserInfor;
import com.app.micaihu.bean.infor.UserTask;
import com.app.micaihu.bean.task.RewardConscription;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.ConfigKeyConstant;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.custom.view.RankLevelView;
import com.app.micaihu.custom.view.SignLastingView;
import com.app.micaihu.custom.view.scrollview.HorizontalScrollViewWithListener;
import com.app.micaihu.custom.view.scrollview.ScrollViewListener;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.AppConfigManager;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.ApiServiceUtils;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.utils.ParamsMapUtils;
import com.app.micaihu.view.bean.SignGold;
import com.app.micaihu.view.main.MainActivity;
import com.app.micaihu.view.user.dialog.SignSuccessDialog;
import com.app.utils.biz.Biz;
import com.app.utils.util.DateUtils;
import com.app.utils.util.StringUtils;
import com.app.utils.util.ToastUtils;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.osutils.StatusBarUtils;
import com.app.utils.util.view.pulltozoom.OnMyScrollListener;
import com.app.utils.util.view.pulltozoom.PullToZoomScrollViewEx;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ViewUtils;
import com.google.gson.reflect.TypeToken;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Guo on 2016/7/8.
 * 任务界面
 */
public class UserTaskActivity extends BaseOldActivity implements View.OnClickListener {


    /**
     * 军衔的view
     */
    private RankLevelView rankLevelView;

    /**
     * 军衔等级差值
     */
    private TextView tvUserTaskLevelDiff;

    /**
     * 副官图片
     */
    private ImageView sdvAdjutant;
    /**
     * 刷新副官语录的linearlayout
     */
    private LinearLayout flushAdjutantText;
    /**
     * 副官语录刷新的progressbar
     */
    private ProgressBar pbAdjutantText;
    /**
     * 副官语录的View
     */
    private TextView tvAdjutantQuotationText;

    /**
     * 军功String
     */
    private String militaryExploit;
    /**
     * 金币String
     */
    private String militaryPay;
    /**
     * 经验差String 百分比
     */
    private float militaryExploitDiff;

    /**
     * 经验差String txt
     */
    private String militaryDiff;

    /**
     * 是否第一次登录boolean
     */
    private boolean isFirstLogin;
    /**
     * 是否签到boolean
     */
    private boolean isSign;
    /**
     * 当前军衔tv
     */
    private String rankName;
    /**
     * 下一级军衔tv
     */
    private String nextRankName;
    /**
     * 当前军衔的icon
     */
    private String rankIcon;
    /**
     * 下一级军衔的icon
     */
    private String nextRankIcon;

    /**
     * 任务登录
     */
    private TextView tvTaskLogin;
    /**
     * 任务邀请
     */
    private TextView tvTaskInvitation;
    /**
     * 任务签到
     */
    private TextView tvTaskSign;
    /**
     * 任务评论
     */
    private TextView tvTaskComment;
    /**
     * 任务分享
     */
    private TextView tvTaskShare;
    /**
     * 下载游戏
     */
    private TextView taskDownload;

    /**
     * 任务完成/未完成 的drawable
     */
    private Drawable yesDrawable;
    private Drawable shareDrawable;
    private Drawable inviteDrawable;
    private Drawable commentDrawable;
    private Drawable signDrawable;
    private Drawable loginDrawable;
    private Drawable readDrawable;
    private Drawable gameDownloadDrawable;

    private String taskMsg;
    private CustomImageView civLeftRank;
    private CustomImageView civRightRank;

    /**
     * 设置下一条副官语录
     */
    private int leng, allLength;//副官语录的文字长度
    private TextView tvTopPay;//金币获取进度
    private TextView tvBottomPay;//当前总金币
    private TextView tvTopExploit;//军功获取进度
    private TextView tvBottomExploit;//当前总军功

    //作战奖励的图标
    private TextView tvTaskBindPhone;
    //private TextView tvTaskAttionWX;//关注微信
    private TextView tvTaskSign7;
    private TextView tvTaskSign30;
    private TextView tvTaskinvite10friends;

    //作战奖励的 领取按钮
    private TextView tvBindPhonePrice;
    //private TextView tvAttionWXPrice;//关注微信的奖励领取
    private TextView tvSign7Price;
    private TextView tvSign30Price;
    private TextView tvInvite10Price;

    //按钮的view数组
    private TextView[] views = new TextView[4];

    //保存day/max军功金币的string字段
    private String dayExploitMax;
    private String dayPayMax;
    private String dayExploit;
    private String dayPay;

    //    private TextView tvSignDot;//签到小红点
    private TextView tvSign;//签到
    private LinearLayout noLoginLayout;//伪登录的layout
    private LinearLayout loginLayout;//已登录的layout
    private SignLastingView slvSignView;

    private List<SignLasting> signList;
    private TextView tvSignPrompt;
    private String signDays;
    private PullToZoomScrollViewEx pullToZoomScrollViewEx;
    private View viewBg;
    private Drawable background;
    private View title;
    private Drawable drawable;
    private TextView tvSignPrompt2;
    private boolean isSignIn7;//是否完成签7天的任务
    private LinearLayout llPrice;
    private AnimationDrawable anim;
    private RelativeLayout headerTitle;
    //    private LinearLayout conscriptionContainer;//征兵奖励的容器
    private HorizontalScrollViewWithListener horizontalScrollView;
    private int conscriptionX = 0;
    private LinearLayout zhengbingContainer;
    private LinearLayout size5Container;
    private LinearLayout size6Container;
    private TextView taskRead;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isInit = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usertask);
        LiveEventBus.get(Config.EventBus.taskSuccess, Boolean.class).observe(this, new androidx.lifecycle.Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean loginSuccess) {
                ViewUtils.runOnUiThreadDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initViewData();
                    }
                }, 100);

            }
        });
        initView();
        initListener();
        isFristLoadMsg();
        Config.addGold = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.e("onRewardVideoAdLoad", "onResume-->" + Config.addGold);
        if (UserInforManager.getInstance().isLogin()) {
            UserInforManager.getInstance().getUserTask(UserTaskActivity.this, false);
        }
        initViewData();
        requestConscriptionTask();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.e("onRewardVideoAdLoad", "onRestart-->" + Config.addGold);
        if (Config.addGold) {
            addGold();
        }
    }

    @SuppressLint("AutoDispose")
    private void addGold() {
        ArrayMap<String, Object> params = ParamsMapUtils.getCommonParams();
        params.put("uid", Biz.getInstance().uid());
        ApiServiceUtils.getApiService().videoGold(params).compose(RxLifecycleUtils.Companion.ioMainScheduler()).subscribe(new NetObserver<BaseBean<SignGold>>() {
            @Override
            public void onSuccess(BaseBean<SignGold> result) {
                LogUtils.e("BaseBean->" + result);
                SignGold data = result.getData();
                if (data != null) {
                    onResume();
                }
            }

            @Override
            public void onFailure(@org.jetbrains.annotations.Nullable Throwable e) {

            }
        });
    }

    /**
     * 初始化 视图数据
     */
    private void initViewData() {
        //判断是否登录状态
        boolean isLogin = UserInforManager.getInstance().isLogin();
        if (isLogin) {//登陆
            noLoginLayout.setVisibility(View.GONE);
            loginLayout.setVisibility(View.VISIBLE);

            UserInfor userInfor = UserInforManager.getInstance().getUserInfor();
            UserTask task = userInfor.getTask();
            //拿取任务的数据
            initTaskData(task);
            if (task.getSignIn()) {
                tvSign.setText(getResources().getString(R.string.signed_text));
                tvSign.setSelected(true);
            } else {
                tvSign.setText(getResources().getString(R.string.sign_text));
                tvSign.setSelected(false);
            }
            tvSignPrompt.setText(Html.fromHtml("您已连续签到" + "<font color=\"#f8630c\">" + signDays + "</font>" + "天"));

            tvBottomExploit.setText(getString(R.string.today_reward) + dayExploit + getString(R.string.today_unreward) + dayExploitMax);
            tvBottomPay.setText(getString(R.string.today_reward) + dayPay + getString(R.string.today_unreward) + dayPayMax);


            //当前所获得的军功金币

            setTaskReward(tvTopExploit, militaryExploit, getString(R.string.user_militaryexploit));
            setTaskReward(tvTopPay, militaryPay, getString(R.string.user_militarypay));


            tvUserTaskLevelDiff.setText("升级还需要" + militaryDiff + "军功");
            //军衔图标
            initRankView(militaryExploitDiff, rankName, nextRankName);

            initSingView(signList);

//            if(isSignIn7){
//                tvSignPrompt2.setText(getString(R.string.signed_str30));
//            }
            if (!TextUtils.isEmpty(task.getSignNotice())) {
                tvSignPrompt2.setText(task.getSignNotice());
            }
        } else {//没登录
            //军功金币
            initSingView(null);
            noLoginLayout.setVisibility(View.VISIBLE);
            loginLayout.setVisibility(View.GONE);
            tvSign.setText(getResources().getString(R.string.sign_text));
            tvSign.setSelected(false);
            tvSignPrompt.setText(Html.fromHtml("<font color=\"#333333\">" + "您以连续签到" + "</font>" + "<font color=\"#f8630c\">0</font><font color=\"#333333\">" + "天</font>"));
            tvSignPrompt2.setText(getString(R.string.signed_str));

        }

        showLoginIcon(isLogin);


    }

    private void initSingView(List<SignLasting> signList) {
        slvSignView.setSignList(signList);

    }


    /**
     * 修正军衔图标的位置
     *
     * @param civLeftRank
     * @param civRightRank
     */
    private void initIcon(CustomImageView civLeftRank, CustomImageView civRightRank) {
        if (civLeftRank == null || civRightRank == null) return;

        LoadImageUtils.getInstance().loadImage(civLeftRank, rankIcon);
        LoadImageUtils.getInstance().loadImage(civRightRank, nextRankIcon);


        ViewSizeUtils.changeViewAuto1080(this.civLeftRank, 50, 50);
        ViewSizeUtils.changeViewAuto1080(this.civRightRank, 50, 50);

        RelativeLayout.LayoutParams param1 =
                new RelativeLayout.LayoutParams(civLeftRank.getLayoutParams().width, civLeftRank.getLayoutParams().height);
        int widthPixels = ViewSizeUtils.getWidthPixels();
        int marginLeft = (int) ((widthPixels / 24f) * 3) - (civLeftRank.getLayoutParams().width / 2);
        param1.leftMargin = marginLeft;
        param1.addRule(RelativeLayout.CENTER_VERTICAL);
        civLeftRank.setLayoutParams(param1);
        civLeftRank.invalidate();
        civLeftRank.requestLayout();


        RelativeLayout.LayoutParams param2 =
                new RelativeLayout.LayoutParams(civRightRank.getLayoutParams().width, civRightRank.getLayoutParams().height);
        param2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        param2.addRule(RelativeLayout.CENTER_VERTICAL);
        int marginRight = (int) (((widthPixels / 24f) * 21) + (civRightRank.getLayoutParams().width / 2));
        param2.rightMargin = widthPixels - marginRight;
        civRightRank.setLayoutParams(param2);
        civRightRank.invalidate();
        civRightRank.requestLayout();

    }


    /**
     * 改变字体大小的方法
     */
    private void setTaskReward(TextView textView, String day, String total) {
        if (day == null || TextUtils.isEmpty(total)) return;
//        String totalStr = day + "/" + total;
        String totalStr = day + " " + total;
        String bigStr = day;
        SpannableString styledText = new SpannableString(totalStr);
        styledText.setSpan(new TextAppearanceSpan(this, R.style.style_day_txt), 0, bigStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText.setSpan(new TextAppearanceSpan(this, R.style.style_max_txt), bigStr.length(), totalStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        if (textView != null) {
            textView.setText(styledText, TextView.BufferType.SPANNABLE);
        }


    }


    /**
     * 是否显示 登录状态的任务
     *
     * @param login
     */
    private void showLoginIcon(boolean login) {
        //展示是否领取奖励的logo方法
//        showRewardLogo(login);
        if (login) {
            //首次登陆
            selectorDrawable(tvTaskLogin, null, isFirstLogin);
            //签到
            selectorDrawable(tvTaskSign, null, isSign);
            UserInfor userInfor = UserInforManager.getInstance().getUserInfor();
            if (userInfor != null && userInfor.getTask() != null) {
                //邀请
                selectorDrawable(tvTaskInvitation, null, userInfor.getTask().getInviteState());
                //评论
                selectorDrawable(tvTaskComment, null, userInfor.getTask().getCommentState());
                //分享
                selectorDrawable(tvTaskShare, null, userInfor.getTask().getShareState());

                selectorDrawable(taskRead, null, userInfor.getTask().getRedState());

                selectorDrawable(taskDownload, null, userInfor.getTask().getGameDownloadState());
            }
        } else {
            //首次登陆
            selectorDrawable(tvTaskLogin, loginDrawable, login);
            //签到
            selectorDrawable(tvTaskSign, signDrawable, login);
            //邀请
            selectorDrawable(tvTaskInvitation, inviteDrawable, login);
            //评论
            selectorDrawable(tvTaskComment, commentDrawable, login);
            //分享
            selectorDrawable(tvTaskShare, shareDrawable, login);

            selectorDrawable(taskDownload, gameDownloadDrawable, login);

            selectorDrawable(taskRead, readDrawable, login);
        }
    }

    /**
     * 设置图标的selector
     *
     * @param view1
     * @param flag
     */
    private void selectorDrawable(TextView view1, Drawable drawable, boolean flag) {
        if (flag) {
            if (yesDrawable == null) return;
            view1.setCompoundDrawables(null, yesDrawable, null, null);
            view1.setTextColor(getResources().getColor(R.color.usertask_price));

        } else {
            if (drawable == null) return;
            view1.setCompoundDrawables(null, drawable, null, null);
            view1.setTextColor(getResources().getColor(R.color.common_font_color_2));
        }
    }

    /**
     * 初始化任务数据
     *
     * @param task
     */
    private void initTaskData(@NonNull UserTask task) {
        //头部的数据

        if (!TextUtils.isEmpty(task.getMilitaryExploit())) {//我的当前总军功
            militaryExploit = task.getMilitaryExploit();
        }
        if (!TextUtils.isEmpty(task.getMilitarPay())) {//我的当前总金币
            militaryPay = task.getMilitarPay();
        }
        if (!TextUtils.isEmpty(task.getMilitaryExploitDiff())) {//经验差百分比
            militaryExploitDiff = StringUtils.toFloat(task.getMilitaryExploitDiff(), 0f);
        }
        if (!TextUtils.isEmpty(task.getMilitaryExploit())) {//经验差
            militaryDiff = task.getMilitaryDiff();
        }

        if (!TextUtils.isEmpty(task.getRankName())) {//当前军衔
            rankName = task.getRankName();
        }
        if (!TextUtils.isEmpty(task.getNextRankName())) {//下一军衔
            nextRankName = task.getNextRankName();
        }
        if (!TextUtils.isEmpty(task.getRankIcon())) {//当前军衔icon
            rankIcon = task.getRankIcon();
        }
        if (!TextUtils.isEmpty(task.getNextRankIcon())) {//下一级军衔的icon
            nextRankIcon = task.getNextRankIcon();
        }

        if (!TextUtils.isEmpty(task.getDayExploitMax())) {//今日可获取的最大军功
            dayExploitMax = task.getDayExploitMax();
        }
        if (!TextUtils.isEmpty(task.getDayPayMax())) {//今日可获取的最大金币
            dayPayMax = task.getDayPayMax();
        }
        if (!TextUtils.isEmpty(task.getDayExploit())) {//今日已获得军功
            dayExploit = task.getDayExploit();
        }
        if (!TextUtils.isEmpty(task.getDayPay())) {//今日已获得金币
            dayPay = task.getDayPay();
        }
        //是否第一次登录
        isFirstLogin = task.getFirstLogin();
        //是否签到
        isSign = task.getSignIn();

        if (task.getSignList() != null && task.getSignList().size() > 0) {
            this.signList = task.getSignList();
        }

        this.signDays = task.getSignDays();

        isSignIn7 = !TextUtils.equals(TypeConstant.CAN_NOT_REWARD, task.getRewardSignSeven());

        if (task.getSignIn()) {
            String currentDayStr = DateUtils.getCurrentTime("yyyy/MM/dd");
            AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.signDay + UserInforManager.getInstance().getUserInfor().getUid(), currentDayStr);
        }

    }

    private SoftReference<Drawable> drawableWeakReference;

    private void initView() {

        //整体的控件
        pullToZoomScrollViewEx = (PullToZoomScrollViewEx) findViewById(R.id.pzsv);

        //头部的变色背景
//        headerTitle = (RelativeLayout) findViewById(R.id.title);

        StatusBarUtils.setTitleMargin(this, R.id.header_title);


//        title =  findViewById(R.id.view_bg);
        title = findViewById(R.id.header_title);
        taskRead = findViewById(R.id.task_read);
//        title.setVisibility(View.VISIBLE);
        title.setOnClickListener(this);
        drawable = getResources().getDrawable(R.color.common_bg_color_1);
        drawableWeakReference = new SoftReference<Drawable>(drawable);
        pullToZoomScrollViewEx.setOverScrollMode(View.OVER_SCROLL_NEVER);

        //header
        llPrice = (LinearLayout) findViewById(R.id.ll_price);

        //登录的控件
        tvUserTaskLevelDiff = (TextView) findViewById(R.id.tv_task_leveldiff);
        rankLevelView = (RankLevelView) findViewById(R.id.rlv_rank_level);
        civLeftRank = (CustomImageView) findViewById(R.id.civ_left_rank);
        civRightRank = (CustomImageView) findViewById(R.id.civ_right_rank);
        //day/max军功金币
        tvTopPay = (TextView) findViewById(R.id.tv_toppay);
        tvBottomPay = (TextView) findViewById(R.id.tv_bottompay);
        tvTopExploit = (TextView) findViewById(R.id.tv_topexploit);
        tvBottomExploit = (TextView) findViewById(R.id.tv_bottomexploit);
        //签到小红点
        //tvSignDot = (TextView) findViewById(R.id.tv_signdot);
        //签到
        tvSign = (TextView) findViewById(R.id.tv_sign);
        tvSignPrompt = (TextView) findViewById(R.id.tv_signprompt);
        tvSignPrompt2 = (TextView) findViewById(R.id.tv_signprompt2);

        //未登录的控件
        noLoginLayout = (LinearLayout) findViewById(R.id.ll_nologin_layout);
        loginLayout = (LinearLayout) findViewById(R.id.ll_login_layout);


        //cotent
        sdvAdjutant = (ImageView) findViewById(R.id.civ_adjutant);
//        tvAdjutantName = (TextView) findViewById(R.id.tv_adjutant_name);
        flushAdjutantText = (LinearLayout) findViewById(R.id.ll_adjutant_text_flush);
        pbAdjutantText = (ProgressBar) findViewById(R.id.pb_adjutant_load_progress);
        tvAdjutantQuotationText = (TextView) findViewById(R.id.tv_adjutant_quotations_text);
        //tvTaskRule = (TextView) findViewById(R.id.tv_taskrule);
        //连续签到的view
        slvSignView = (SignLastingView) findViewById(R.id.slv_signview);

        tvTaskLogin = (TextView) findViewById(R.id.task_login);
        tvTaskInvitation = (TextView) findViewById(R.id.task_invitation);
        tvTaskSign = (TextView) findViewById(R.id.task_sign);
        tvTaskComment = (TextView) findViewById(R.id.task_comment);
        tvTaskShare = (TextView) findViewById(R.id.task_share);
        taskDownload = (TextView) findViewById(R.id.task_download);

        tvTaskBindPhone = (TextView) findViewById(R.id.task_bindphone);
        //tvTaskAttionWX = (TextView) findViewById(R.id.task_attionwx);
        tvTaskSign7 = (TextView) findViewById(R.id.task_sign7);
        tvTaskSign30 = (TextView) findViewById(R.id.task_sign30);
        tvTaskinvite10friends = (TextView) findViewById(R.id.task_invite10friends);


        tvBindPhonePrice = (TextView) findViewById(R.id.task_bindphone_price);
        //tvAttionWXPrice = (TextView) findViewById(R.id.task_attionwx_price);
        tvSign7Price = (TextView) findViewById(R.id.task_sign7_price);
        tvSign30Price = (TextView) findViewById(R.id.task_sign30_price);
        tvInvite10Price = (TextView) findViewById(R.id.task_invite10friends_price);

//        views[0] = tvBindPhonePrice;
//        views[1] = tvAttionWXPrice;
//        views[2] = tvSign7Price;
//        views[3] = tvSign30Price;
//        views[4] = tvInvite10Price;
        views[0] = tvBindPhonePrice;
        //views[1] = tvAttionWXPrice;
        views[1] = tvSign7Price;
        views[2] = tvSign30Price;
        views[3] = tvInvite10Price;

        //加载虎妞
//        LoadImageUtils.getInstance().loadImageGif(sdvAdjutant, R.drawable.pic_fight_pepo_normal);

        anim = null;
        anim = (AnimationDrawable) sdvAdjutant.getDrawable();
        sdvAdjutant.post(new Runnable() {

            @Override
            public void run() {
                anim.start();
            }
        });
        sdvAdjutant.setDrawingCacheBackgroundColor(0xffff0000);//设置RPG565节约内存
//        setAnimationCacheEnabled(false);//禁止view的动画缓存
        //初始化drawable
        initDrawable();

        //征兵奖励的容器
//        conscriptionContainer = (LinearLayout) findViewById(R.id.ll_conscription_container);
        size5Container = (LinearLayout) findViewById(R.id.size5Contianer);
        size6Container = (LinearLayout) findViewById(R.id.size6Contianer);
        horizontalScrollView = (HorizontalScrollViewWithListener) findViewById(R.id.hScrollView);

        zhengbingContainer = (LinearLayout) findViewById(R.id.zhengbingContainer);

        boolean isShowGame = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.isGameShow, false);
        if (isShowGame) {
            findViewById(R.id.llGameDownload).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.llGameDownload).setVisibility(View.GONE);
        }
    }


    /**
     * 为征兵的Scroll设置listener
     */
    private void setZhengbingListener() {
        horizontalScrollView.setScrollViewListener(new ScrollViewListener() {


            @Override
            public void onScrollChanged(HorizontalScrollViewWithListener scrollView, int x, int y, int oldx, int oldy) {
                conscriptionX = x;

            }
        });
    }


    private void initListener() {
        flushAdjutantText.setOnClickListener(this);
        tvAdjutantQuotationText.setOnClickListener(this);

        tvSign.setOnClickListener(this);

        tvTaskLogin.setOnClickListener(this);
        tvTaskInvitation.setOnClickListener(this);
        tvTaskSign.setOnClickListener(this);
        tvTaskComment.setOnClickListener(this);
        tvTaskShare.setOnClickListener(this);
        taskDownload.setOnClickListener(this);
        rankLevelView.setOnClickListener(this);

        noLoginLayout.setOnClickListener(this);

        tvTaskBindPhone.setOnClickListener(this);
        //tvTaskAttionWX.setOnClickListener(this);
        tvTaskSign7.setOnClickListener(this);
        tvTaskSign30.setOnClickListener(this);
        tvTaskinvite10friends.setOnClickListener(this);
        llPrice.setOnClickListener(this);
        taskRead.setOnClickListener(this);

        pullToZoomScrollViewEx.setScrollListener(transparentListener);


    }

    boolean isBGFull;
    private OnMyScrollListener transparentListener = new OnMyScrollListener() {
        @Override
        public void onScroll(int scrollY) {
            if (scrollY > 255) {
                scrollY = 255;
                if (!isBGFull) {
                    if (drawableWeakReference.get() != null) {
                        drawableWeakReference.get().setAlpha(scrollY);
                    } else {
                        if (drawable != null)
                            drawable.setAlpha(scrollY);
                    }
                    if (android.os.Build.VERSION.SDK_INT >= 16) {
                        if (drawableWeakReference.get() != null) {
                            title.setBackground(drawableWeakReference.get());
                        } else {
                            if (drawable != null)
                                title.setBackground(drawable);
                        }
                    } else {
                        if (drawableWeakReference.get() != null) {
                            title.setBackgroundDrawable(drawableWeakReference.get());
                        } else {
                            if (drawable != null)
                                title.setBackgroundDrawable(drawable);
                        }
                    }

                }
                isBGFull = true;
            } else {
                isBGFull = false;
                if (drawableWeakReference.get() != null) {
                    drawableWeakReference.get().setAlpha(scrollY);
                } else {
                    if (drawable != null)
                        drawable.setAlpha(scrollY);
                }
                if (android.os.Build.VERSION.SDK_INT >= 16) {
                    if (drawableWeakReference.get() != null) {
                        title.setBackground(drawableWeakReference.get());
                    } else {
                        if (drawable != null)
                            title.setBackground(drawable);
                    }
                } else {
                    if (drawableWeakReference.get() != null) {
                        title.setBackgroundDrawable(drawableWeakReference.get());
                    } else {
                        if (drawable != null)
                            title.setBackgroundDrawable(drawable);
                    }
                }
            }
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_bg:
                break;
            case R.id.ll_nologin_layout://点击头像->个人主页/登录
                UserInforManager.getInstance().toLoginPage(this);
                break;

            case R.id.tv_sign://点击签到->签到/登录
                if (UserInforManager.getInstance().isLogin()//已经登录且没有签到的
                        && !UserInforManager.getInstance().getUserInfor().getTask().getSignIn()) {
                    signOperat();
                } else if (!UserInforManager.getInstance().isLogin()) {
                    //登录
                    UserInforManager.getInstance().toLoginPage(this);
                } else if (UserInforManager.getInstance().isLogin()
                        && UserInforManager.getInstance().getUserInfor().getTask() != null
                        && UserInforManager.getInstance().getUserInfor().getTask().getSignIn()) {
                    //toPriceWebActivity();
                    ToastUtils.makeText(getResources().getString(R.string.signed_text));
                }
                StatService.onEvent(this, "008", "签到", 1);//统计事件的点击次数
                break;
            case R.id.ll_adjutant_text_flush://点击刷新副官语录
            case R.id.tv_adjutant_quotations_text:
                setNextSignAdjutantText();
                StatService.onEvent(this, "050", "副官语录刷新", 1);// 统计的点击
                break;
            /**************任务图标**************/
            case R.id.task_login://点击 ",每日登陆"
                toTaskDetails(URIContant.TASK_LOGIN, UserInforManager.getInstance().isLogin());
                StatService.onEvent(this, "050", "查看每日登录任务", 1);//统计事件的点击次数
                break;
            case R.id.task_invitation://点击 "每日邀请"
                Intent intent1 = new Intent(mActivity, ConscriptionRewardH5Activity.class);
                if (UserInforManager.getInstance().isLogin()) {
                    intent1.putExtra("url", URIContant.TASK_CONSCRIPTION + UserInforManager.getInstance().getUserInfor().getUid());
                    intent1.putExtra("uid", UserInforManager.getInstance().getUserInfor().getUid());
                } else {
                    intent1.putExtra("url", URIContant.TASK_CONSCRIPTION);
                }
                ActivityUtils.startActivity(intent1);
                StatService.onEvent(this, "050", "查看每日邀请任务", 1);//统计事件的点击次数
                break;
            case R.id.task_sign://点击 "每日签到"
                toTaskDetails(URIContant.TASK_SIGNIN, UserInforManager.getInstance().isLogin());
                StatService.onEvent(this, "050", "查看每日签到任务", 1);//统计事件的点击次数
                break;
            case R.id.task_read://点击 "阅读"
                toTaskDetails(URIContant.TASK_READ, UserInforManager.getInstance().isLogin());
                StatService.onEvent(this, "050", "查看阅读任务", 1);//统计事件的点击次数
                break;
            case R.id.task_comment://点击 "每日评论"
                toTaskDetails(URIContant.TASK_COMMENT, UserInforManager.getInstance().isLogin());
                StatService.onEvent(this, "050", "查看每日评论任务", 1);//统计事件的点击次数
                break;
            case R.id.task_download://点击 "下载游戏"
                toTaskDetails(URIContant.TASK_GAMEDOWNLOAD, UserInforManager.getInstance().isLogin());
                StatService.onEvent(this, "050", "查看下载游戏任务", 1);//统计事件的点击次数
                break;
            case R.id.task_share://点击 "每日分享"
                toTaskDetails(URIContant.TASK_SHARE, UserInforManager.getInstance().isLogin());
                StatService.onEvent(this, "050", "查看每日分享任务", 1);//统计事件的点击次数
                break;
            case R.id.rlv_rank_level://自定义的军衔
                UserInforManager.getInstance().toWebActivity(this, "军衔等级", URIContant.MILITARY_RANK + "?uid=" + (UserInforManager.getInstance().isLogin() ? UserInforManager.getInstance().getUserInfor().getUid() : ""));
                StatService.onEvent(this, "050", "军衔等级明细", 1);//统计事件的点击次数
                break;

            /**************奖励图标--跳转webview***************/
            case R.id.task_bindphone://绑定手机图标
                startActivity(new Intent(this, TaskRewardBindPhoneActivity.class));
                StatService.onEvent(this, "050", "查看绑定手机任务", 1);//统计事件的点击次数
                break;
            case R.id.task_sign7://签到7天
                tempWebView(URIContant.TASK_SIGN7DAY);
                StatService.onEvent(this, "050", "查看签到7天任务", 1);//统计事件的点击次数
                break;
            case R.id.task_sign30://签到30天
                tempWebView(URIContant.TASK_SIGN30DAY);
                StatService.onEvent(this, "050", "查看签到30天任务", 1);//统计事件的点击次数
                break;
            case R.id.task_invite10friends://邀请好友
                tempWebView(URIContant.TASK_INVITE10);
                StatService.onEvent(this, "050", "查看邀请10人任务", 1);//统计事件的点击次数
                break;

            /**************领取奖励---领取任务奖励类型 1绑定手机 2签到七天 3签到30天 4邀请10位好友***************/
            case R.id.task_bindphone_price://领取 绑定手机 1
                getReward(v, 1, 0);
                StatService.onEvent(this, "050", "领取绑定手机奖励", 1);//统计事件的点击次数
                break;
//            case R.id.task_attionwx_price://领取关注微信
//                getReward(1);
//                break;
            case R.id.task_sign7_price://领取7天签到 2
                getReward(v, 2, 0);
                StatService.onEvent(this, "050", "领取签到7天奖励", 1);//统计事件的点击次数
                break;
            case R.id.task_sign30_price://领取30天签到 3
                getReward(v, 3, 0);
                StatService.onEvent(this, "050", "领取签到30天奖励", 1);//统计事件的点击次数
                break;
            case R.id.task_invite10friends_price://领取邀请10人 4
                getReward(v, 4, 0);
                StatService.onEvent(this, "050", "领取邀请10人奖励", 1);//统计事件的点击次数
                break;

            case R.id.ll_price://奖励明细
                if (UserInforManager.getInstance().isLogin()) {
                    tempWebView(UserInforManager.getInstance().getUserInfor().getTask().getRewardUrl());
                    StatService.onEvent(this, "050", "军功金币奖励明细", 1);// 统计的点击
                }
                break;


            case R.id.conscriptionIcon://征兵
//                http://page.we630.com/mc/levySolider?uid=

                Intent intent = new Intent(mActivity, ConscriptionRewardH5Activity.class);
                if (UserInforManager.getInstance().isLogin()) {
                    intent.putExtra("url", URIContant.TASK_CONSCRIPTION + UserInforManager.getInstance().getUserInfor().getUid());
                    intent.putExtra("uid", UserInforManager.getInstance().getUserInfor().getUid());

                } else {
                    intent.putExtra("url", URIContant.TASK_CONSCRIPTION);
                }

                UserTaskActivity.this.startActivity(intent);
                break;

//            case R.id.conscriptionBtn:
//                try{
//                    if(v.getParent() instanceof RewardView){
//
//                        RewardView rewardView = (RewardView) v.getParent();
//                        RewardConscription rewardConscription =  rewardView.getRewardConscription();
//                        if(rewardConscription != null){
//                            getReward(rewardView,6,rewardConscription.getInvateNum());
//                        }
//                    }
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//
//                break;

        }
    }

    /**
     * 从服务器领取奖励
     *
     * @param type 类型 1绑定手机 2签到七天 3签到30天 4邀请10位好友
     */
    private void getReward(final View view, final int type, int inviteNum) {
        boolean login = UserInforManager.getInstance().isLogin();
        if (!login) {
            UserInforManager.getInstance().toLoginPage(UserTaskActivity.this);
        } else {
            ParamMap params = new ParamMap();
            params.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());
            params.putEncode("type", type + "");
            params.putEncode("inviteNum", inviteNum + "");
            HttpUtils.ParmsMapMD5(params);
            HttpListener<DataBean<GetTaskBean>> getRewardListener = new HttpListener<DataBean<GetTaskBean>>() {
                @Override
                public void onStart() {
                    super.onStart();
                    DialogUtils.getInstance().showProgressDialog(UserTaskActivity.this, getResources().getString(R.string.task_getreward));
                }

                @Override
                public void onError(VolleyError error) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    ToastUtils.makeText(getResources().getString(R.string.neterror));

                }

                @Override
                public void onSuccess(DataBean<GetTaskBean> response) {
                    DialogUtils.getInstance().dismissProgressDialog();
                    if (!response.noError()) {
                        if (TextUtils.equals(response.getCode(), "127")) {
                            ToastUtils.makeText(getResources().getString(R.string.task_alreadyreceive));
                        } else {
                            ToastUtils.makeText(response.getMsg());
                        }
                    } else {
                        //成功了
                        if (view == null) return;
                        if (response.getData().getIsGetTask("征兵")) {
                            UserInforManager.getInstance().getUserTask(UserTaskActivity.this, false);
                            switch (type) {
//                            case 1://绑定手机
////                            if(tvBindPhonePrice == null) return;
//                                setRewardComplete((TextView) view);
//
//                                break;
//                            case 2://签到7天
////                                if(tvSign7Price == null) return;
//                                setRewardComplete((TextView) view);
//                                break;
//                            case 3://签到30天
////                                if(tvSign30Price == null) return;
//                                setRewardComplete((TextView) view);
//                                break;
//                            case 4://邀请10人
////                                if(tvInvite10Price == null) return;
//                                setRewardComplete((TextView) view);
//                                break;
                                case 6://征兵的  领取奖励
                                    try {
                                        RewardView rewardView = (RewardView) view;
                                        rewardView.refreshStatus(RewardView.rewarded);
                                    } catch (Exception e) {

                                    }

                                    break;

                            }
                        }


                    }
                }
            };

            addPostRequest(URIContant.GETREWARD, new TypeToken<DataBean<GetTaskBean>>() {
            }.getType(), params, getRewardListener);
        }

    }


    /**
     * 临时的 跳转奖励规则的方法
     *
     * @param url
     */
    private void tempWebView(String url) {
        UserInforManager.getInstance().toWebActivity(this, "奖励规则", url);
    }


    /**
     * 去往任务详情的H5动作
     *
     * @param url
     * @param isLogin
     */
    private void toTaskDetails(String url, boolean isLogin) {
        UserInforManager.getInstance().toWebActivity(this, "任务详情", url + "?uid=" + (isLogin ? UserInforManager.getInstance().getUserInfor().getUid() : ""));
    }

    /**
     * 签到动作
     */
    private void signOperat() {

        ParamMap params = new ParamMap();
        params.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());
        HttpUtils.ParmsMapMD5(params);
        addPostRequest(URIContant.SIGNIN, new TypeToken<DataBean<TaskSignBean>>() {
        }.getType(), params, new HttpListener<DataBean<TaskSignBean>>() {
            @Override
            public void onStart() {
                super.onStart();
                DialogUtils.getInstance().showProgressDialog(UserTaskActivity.this, AppApplication.getInstance().getResources().getString(R.string.task_sign));
            }

            @Override
            public void onError(VolleyError error) {
                ToastUtils.makeText(AppApplication.getInstance().getResources().getString(R.string.neterror));
                DialogUtils.getInstance().dismissProgressDialog();
            }

            @Override
            public void onSuccess(DataBean<TaskSignBean> response) {
                DialogUtils.getInstance().dismissProgressDialog();
                if (!response.noError()) {
                    ToastUtils.makeText(response.getNnderstoodMsg());
                } else {
                    TaskSignBean taskSignBean = response.getData();
                    if (taskSignBean != null) {
                        if (taskSignBean.getIsGetTask()) {//拉取任务
                            UserInforManager.getInstance().getUserTask(UserTaskActivity.this, false);
                        }
                        tvSign.setText(AppApplication.getInstance().getResources().getString(R.string.signed_text));
                        tvSign.setSelected(true);

//                        //根据连续签到的天数显示不同的弹窗
//                        if(TextUtils.equals(taskSignBean.getContinueSignDay(),"7")
//                                && UserInforManager.getInstance().isLogin()
//                                && !TextUtils.equals(UserInforManager.getInstance().getUserInfor().getTask().getRewardSignSeven()
//                                ,TypeConstant.REWARDED)){
//                            Handler handler = new Handler(){
//                                @Override
//                                public void handleMessage(Message msg) {
//                                    getReward(tvSign7Price,2,0);
//                                }
//                            };
//                            com.app.micaihu.custom.view.dialog.CustomDialog.getInstance().showAlwaysSignpDialog(UserTaskActivity.this, "7",handler);
//                        }else if(TextUtils.equals(taskSignBean.getContinueSignDay(),"30")
//                                && UserInforManager.getInstance().isLogin()
//                                && !TextUtils.equals(UserInforManager.getInstance().getUserInfor().getTask().getRewardSignSeven()
//                                ,TypeConstant.REWARDED)){
//                            Handler handler = new Handler(){
//                                @Override
//                                public void handleMessage(Message msg) {
//                                    getReward(tvSign30Price,3,0);
//                                }
//                            };
//                            com.app.micaihu.custom.view.dialog.CustomDialog.getInstance().showAlwaysSignpDialog(UserTaskActivity.this, "30",handler);
//                        }else{
//                        }
                        //通知主界面隐藏小红点

//                        com.app.micaihu.custom.view.dialog.CustomDialog.getInstance().showSignpDialog(UserTaskActivity.this, taskSignBean.getReward(), taskSignBean.getExtraRewardTitle(), taskSignBean.getExtraRewardContent());

                        Bundle bundle = new Bundle();
                        bundle.putString(Config.Extra.gold, taskSignBean.getReward());
                        signSuccessDialog.setArguments(bundle);
                        signSuccessDialog.show(UserTaskActivity.this);
                        signSuccessDialog.setOnOpenClick(new SignSuccessDialog.OnOpenClick() {
                            @Override
                            public void onAddGold(int gold) {
                                onResume();
                            }
                        });
                        ArrayList<Object> parms = new ArrayList<>();
                        parms.add("6");
                        EventBus.getDefault().post(parms);
                        //记录签到的日期
                        String currentDayStr = DateUtils.getCurrentTime("yyyy/MM/dd");
                        AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.signDay + UserInforManager.getInstance().getUserInfor().getUid(), currentDayStr);
                    }
                }
            }
        });


    }

    private SignSuccessDialog signSuccessDialog = new SignSuccessDialog();
//    /**
//     * 跳转抽奖页面
//     */
//    private void toPriceWebActivity() {
//        Intent intent = new Intent(this, WebActivity.class);
//        intent.putExtra("title", "抽奖");
//        String url = URIContant.TASK_PRIZE + UserInforManager.getInstance().getUserInfor().getUid();
//        intent.putExtra("url", url);
//        intent.putExtra("isRemoveSilde", true);
//        intent.putExtra("isanim",true);
//        //mActivity.startActivityForResult(intent,REQUESTCODE_1);
//        startActivity(intent);
//        overridePendingTransition(R.anim.activity_scale_translate, R.anim.activity_null);
//    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == UserInforManager.TO_LOGIN_REQUESTCODE//从登录回来的
                && resultCode == RESULT_OK) {
            UserInforManager.getInstance().showLevelDialog(UserTaskActivity.this);
        }

    }

    /**
     * 获取副官语录
     */
    private void getAdjutantMsg(final int num) {
        Map<String, String> params = new HashMap<>();
        if (UserInforManager.getInstance().isLogin()) {
            params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        }
        params.put("type", num + "");


        addPostRequest(URIContant.ADJUTANT, new TypeToken<DataBean<AdjutantMsg>>() {
        }.getType(), params, new HttpListener<DataBean<AdjutantMsg>>() {
            @Override
            public void onStart() {
                super.onStart();
                tvAdjutantQuotationText.setVisibility(View.GONE);
                pbAdjutantText.setVisibility(View.VISIBLE);

            }

            @Override
            public void onError(VolleyError error) {
                tvAdjutantQuotationText.setVisibility(View.VISIBLE);
                pbAdjutantText.setVisibility(View.GONE);
                tvAdjutantQuotationText.setText(AppApplication.getInstance().getResources().getString(R.string.adjutant_error));

            }

            @Override
            public void onSuccess(DataBean<AdjutantMsg> response) {
                if (!response.noError()) {
                    tvAdjutantQuotationText.setVisibility(View.VISIBLE);
                    pbAdjutantText.setVisibility(View.GONE);

                    tvAdjutantQuotationText.setText(AppApplication.getInstance().getResources().getString(R.string.adjutant_error));

                } else {
                    tvAdjutantQuotationText.setVisibility(View.VISIBLE);
                    pbAdjutantText.setVisibility(View.GONE);
                    AdjutantMsg adjutantMsg = response.getData();
                    if (adjutantMsg != null && !TextUtils.isEmpty(adjutantMsg.getQuoteInfo())) {
                        taskMsg = adjutantMsg.getQuoteInfo();
                        //String s = toDBC(taskMsg);
                        tvAdjutantQuotationText.setText(taskMsg);
                    }
                    AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.adjutantText, num + 1);
                }

            }
        });
    }


    /**
     * 设置副官语录
     */
    private void setNextSignAdjutantText() {
        if (taskMsg == null) {
            return;
        }
        Layout lt = tvAdjutantQuotationText.getLayout();
        if (lt == null) {
            //联网  得到下一条数据

            int num = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.adjutantText, 1);
            allLength = 0;
            getAdjutantMsg(num);
            return;
        }
        int topOfLastLine = tvAdjutantQuotationText.getHeight() - tvAdjutantQuotationText.getPaddingTop() - tvAdjutantQuotationText.getPaddingBottom() - tvAdjutantQuotationText.getLineHeight();
        int lastLineIndex = 0;
        try {
            lastLineIndex = lt.getLineForVertical(topOfLastLine);
            leng = lt.getLineEnd(lastLineIndex);
            allLength += leng;
        } catch (Exception e) {
            //联网  得到下一条数据
            int num = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.adjutantText, 1);
            allLength = 0;
            getAdjutantMsg(num);
            return;
        }
        String s = tvAdjutantQuotationText.getText().toString();
        if (allLength < s.length()) {
            if (leng > 0) {
                tvAdjutantQuotationText.setText(s.substring(leng - 1));
            } else {
                tvAdjutantQuotationText.setText(s);
            }
        } else {
            //联网  得到下一条数据++++++++++++++++++++++++
            int num = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.adjutantText, 1);
            allLength = 0;
            getAdjutantMsg(num);
        }

    }

    /**
     * 判断是否是今天第一次打开本页面,进行逻辑处理
     */
    private void isFristLoadMsg() {


        String currentDayStr = DateUtils.getCurrentTime("yyyy/MM/dd");
        long currentMillis = DateUtils.simpleDate2Millisecond(currentDayStr);
        long oldMillis = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.dayMillisecond, 0L);
        if (currentMillis > oldMillis) {
            //如果是当天第一次进入
            //记录当日时间
            AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.dayMillisecond, currentMillis);
            //记录副官语录刷新 的次数num
            AppConfigManager.getInstance().writeConfig(ConfigKeyConstant.adjutantText, 1);
            //请求副官语录 参数为1
            getAdjutantMsg(1);
        } else {
            //不是当天第一次进入
            //拿取当天副官语录的刷新的num
            int num = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.adjutantText, 1);
            //设置副官语录,请求次数是当天的num
            getAdjutantMsg(num);//请求副官语录 参数是今日请求次数的累加
        }
    }


    /**
     * 初始化军衔升级的视图
     */
    private void initRankView(float per, String rankName, String nextRankName) {
        if (rankLevelView == null) return;
        rankLevelView.setPersent(per);
        initIcon(civLeftRank, civRightRank);
        rankLevelView.setRankText(rankName, nextRankName);

    }

    private void initDrawable() {
        //完成任务的
        yesDrawable = this.getResources().getDrawable(R.drawable.task_icon_head_yes);
        //登陆
        loginDrawable = this.getResources().getDrawable(R.drawable.task_icon_loading);
        //邀请
        inviteDrawable = this.getResources().getDrawable(R.drawable.task_icon_invite);
        //签到
        signDrawable = this.getResources().getDrawable(R.drawable.task_icon_sign);
        //评论
        commentDrawable = this.getResources().getDrawable(R.drawable.task_icon_comment);
        //分享
        shareDrawable = this.getResources().getDrawable(R.drawable.task_icon_share);

        //阅读
        readDrawable = this.getResources().getDrawable(R.drawable.task_icon_read);

        gameDownloadDrawable = this.getResources().getDrawable(R.drawable.task_icon_game);
        setDrawableBounds(yesDrawable);
        setDrawableBounds(loginDrawable);
        setDrawableBounds(inviteDrawable);
        setDrawableBounds(signDrawable);
        setDrawableBounds(commentDrawable);
        setDrawableBounds(shareDrawable);
        setDrawableBounds(readDrawable);
        setDrawableBounds(gameDownloadDrawable);

    }

    private void setDrawableBounds(Drawable drawable) {
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    }


    /**
     * 释放drawable
     */
    private void destroyDrawable() {
        yesDrawable = null;
        loginDrawable = null;
        inviteDrawable = null;
        signDrawable = null;
        commentDrawable = null;
        shareDrawable = null;
        readDrawable = null;
        gameDownloadDrawable = null;

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroyDrawable();
        if (anim != null && anim.isRunning()) {
            //停止蹦跑动画
            anim.stop();
        }

    }


    @Override
    public void onBackPressed() {
        if (ActivityUtils.isActivityExistsInStack(MainActivity.class)) {
            super.onBackPressed();
        } else {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }


    /**
     * 请求征兵任务
     */
    private void requestConscriptionTask() {
        if (zhengbingContainer == null || size6Container == null || size5Container == null) return;
        ParamMap paramMap = new ParamMap();
        if (UserInforManager.getInstance().isLogin()) {
            paramMap.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());

        } else {
            paramMap.putEncode("uid", "");
        }
        HttpUtils.ParmsMapMD5(paramMap);
        final HttpListener<DataBean<List<RewardConscription>>> conscriptionListener = new HttpListener<DataBean<List<RewardConscription>>>() {
            @Override
            public void onError(VolleyError error) {
                zhengbingContainer.setVisibility(View.GONE);
            }

            @Override
            public void onSuccess(DataBean<List<RewardConscription>> response) {
                if (response.noErrorData()) {

                    LinearLayout conscriptionContainer = new LinearLayout(UserTaskActivity.this);

                    conscriptionContainer.removeAllViews();
                    size6Container.removeAllViews();
                    size5Container.removeAllViews();
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(0, ViewSizeUtils.dp2px(UserTaskActivity.this, 15), 0, ViewSizeUtils.dp2px(UserTaskActivity.this, 15));
                    List<RewardConscription> rewardConscriptionList = response.getData();
                    if (rewardConscriptionList.size() > 0) {
                        zhengbingContainer.setVisibility(View.VISIBLE);
                        //判断,如果 > 5个,就加入到scroll中
                        if (rewardConscriptionList.size() > 5) {
                            horizontalScrollView.setVisibility(View.VISIBLE);
                            size5Container.setVisibility(View.GONE);
                            setZhengbingListener();
                            conscriptionContainer.setPadding(ViewSizeUtils.dp2px(UserTaskActivity.this, 15), 0, 0, 0);
                            size6Container.addView(conscriptionContainer);

                        } else {//否则,加入到FL中,用权重做

                            horizontalScrollView.setVisibility(View.GONE);
                            size5Container.setVisibility(View.VISIBLE);
                            layoutParams.width = ViewSizeUtils.getWidthPixels();
                            size5Container.addView(conscriptionContainer);
                        }

                        conscriptionContainer.setLayoutParams(layoutParams);
                        for (int i = 0; i < rewardConscriptionList.size(); i++) {
                            if (rewardConscriptionList.get(i) != null) {
                                RewardView rewardView = new RewardView(UserTaskActivity.this, rewardConscriptionList.get(i));
                                rewardView.setRewardClickListener(UserTaskActivity.this);
                                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rewardView.getLayoutParams();
                                if (lp == null) {
                                    lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                }
                                if (rewardConscriptionList.size() > 5) {
                                    lp.rightMargin = ViewSizeUtils.dp2px(UserTaskActivity.this, 18);
                                } else {
                                    lp.width = 0;
                                    lp.weight = 1;
                                }
                                rewardView.setLayoutParams(lp);
                                conscriptionContainer.addView(rewardView);
                            }
                        }
                    } else {
                        zhengbingContainer.setVisibility(View.GONE);
                    }
                }
            }
        };

        addPostRequest(URIContant.REQUEST_CONSCRIPTION, new TypeToken<DataBean<List<RewardConscription>>>() {
        }.getType(), paramMap, conscriptionListener);
    }
}
